<template>
  <!-- Form增加 -->
  <el-dialog v-model="dialogFormVisible" title="">
    <el-form :model="form">
      <el-form-item label="详情图" :label-width="formLabelWidth">
        <el-upload
          class="avatar-uploader"
          :action="`${api}/index.php/index/goodses/aliuploadfile`"
          :data="jm()"
          :show-file-list="false"
          :on-success="
            (file) => {
              editImgsProps({ file, key: 'categoryimg ' });
            }
          "
        >
          <img
            v-if="form.categoryimg"
            :src="HttpImgs + form.categoryimg"
            class="avatar"
          />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="详情长图" :label-width="formLabelWidth">
        <el-upload
          class="avatar-uploader"
          :action="`${api}/index.php/index/goodses/aliuploadfile`"
          :data="jm()"
          :show-file-list="false"
          :on-success="
            (file) => {
              editImgsProps({ file, key: 'longimg' });
            }
          "
        >
          <img
            v-if="form.longimg"
            :src="HttpImgs + form.longimg"
            class="avatar"
          />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="postData"> 确定 </el-button>
      </span>
    </template>
  </el-dialog>

  <el-table :data="tableData" style="width: 100%">
    <el-table-column fixed prop="id" label="id" width="120" align="center" />
    <el-table-column
      fixed
      prop="goods_name"
      label="车型"
      width="420"
      align="center"
    />
    <el-table-column
      prop="carbigimgs"
      label="详情图"
      width="150"
      align="center"
    >
      <template #default="scope">
        <img
          style="width: 100px; height: 100px"
          :src="HttpImgs + scope.row.categoryimg"
        />
      </template>
    </el-table-column>
    <el-table-column prop="longimg" label="详情长图" width="150" align="center">
      <template #default="scope">
        <img
          style="width: 100px; height: 100px"
          :src="HttpImgs + scope.row.longimg"
        />
      </template>
    </el-table-column>
    <el-table-column fixed="right" label="操作" align="center">
      <template #default="scope">
        <el-button link type="primary" @click="eid(scope.row.id)"
          >修改</el-button
        >
        <el-button link type="danger" @click="del(scope.row.id)"
          >删除</el-button
        >
      </template>
    </el-table-column>
  </el-table>
</template>
<script>
import UploadFile from "element-plus";
import { ElMessageBox, ElMessage } from "element-plus";
export default {
  components: {
    [UploadFile.name]: UploadFile,
  },
  data() {
    return {
      HttpImgs: "https://ckmco-video.oss-cn-shanghai.aliyuncs.com/",
      tableData: [],
      form: [],
      form1: [],
      dialogFormVisible: false,
      dialogFormVisible1: false,
      formLabelWidth: "120px",
      imageUrl: "",
      options: [
        {
          value: "0",
          label: "未发布",
        },
        {
          value: "1",
          label: "活动中",
        },
      ],
      value: "",
      id: "",
      title: "",
    };
  },
  mounted() {
    this.getData();
  },
  methods: {
    search() {
      this.getData();
    },
    cz() {
      this.title = "";
      this.getData();
    },
    add() {
      this.form.name = "";
      this.form.kjtime = "";
      this.form.cjzt = "";
      this.form.images = "";
      this.form.img = "";
      this.form.title = "";
      this.form.num = "";
      this.dialogFormVisible = true;
      this.id = "";
    },
    del(id) {
      ElMessageBox.confirm("是否确定删除?", "", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http
            .post("/index.php/index/Admincp/cpdel", {
              id: id,
            })
            .then((res) => {
              if (res.code == 200) {
                this.getData();
                ElMessage({
                  message: "删除成功",
                  type: "success",
                });
              }
            });
        })
        .catch(() => {});
    },
    eid(id) {
      this.dialogFormVisible = true;
      this.id = id;
      this.$http
        .post("/index.php/index/Admincp/cpxq", {
          id: id,
        })
        .then((res) => {
          if (res.code == 200) {
            this.form = res.data[0];
            // this.form.kjtime = this.getLocalTime(res.data[0].kjtime)
            // if (res.data[0].cjzt == '0') {
            // 	this.value = "未发布"
            // } else if (res.data[0].cjzt == '1') {
            // 	this.value = "活动中"
            // } else {
            // 	this.value = "已结束"
            // }
          }
        });
    },
    // updateData() {
    // 	let data1 = {
    // 		id: this.form1.id,
    // 		name: this.form1.name,
    // 		cjzt: this.form1.cjzt,
    // 		kjtime:this.getTimeFormat(this.form1.kjtime),
    // 		images: this.form1.images,
    // 		twlink: this.form1.twlink,
    // 	}
    // 	console.log(data1)
    // 	this.$http.post("/index.php/index/goodses/cjupdate", data1).then(res => {
    // 		if (res.code == 200) {
    // 			this.dialogFormVisible1 = false;
    // 			this.getData()
    // 		 }
    // 	 })
    // },
    getData() {
      this.tableData = [];
      this.$http.post("/index.php/index/Admincp/cplist").then((res) => {
        this.tableData = res.data;
        let str =
          '["file/2021/1105/d34e01540ad4b0a0c4a003f0c600355c33751f67.jpeg"]';
        console.log(JSON.parse(str));
        this.tableData.forEach((item) => {
          item.carbigimgs = JSON.parse(item.carbigimg)[0];
        });
      });
    },
    postData() {
      // this.form.cjzt = this.value;
      let data = {
        longimg: this.form.longimg,
        categoryimg: this.form.categoryimg,
        id: this.id,
      };
      this.$http
        .post(
          this.id
            ? "/index.php/index/Admincp/cpupdate"
            : "/index.php/index/goodses/cjadd",
          data
        )
        .then((res) => {
          if (res.code == 200) {
            this.dialogFormVisible = false;
            this.id = "";
            ElMessage({
              message: this.id ? "编辑成功" : "新增成功",
              type: "success",
            });
            this.getData();
          }
        });
    },
    getTimeFormat(zcTime) {
      let time = new Date(zcTime).getTime() / 1000; //除1000 是变成秒级的时间戳 不除就是毫秒级
      return time;
    },
    disabledDateFun(time) {
      return time.getTime() < Date.now() - 8.64e7;
    },
    formatEmployment(type) {
      // 根据源数据经过处理 把处理之后的数据返回
      // 定义一个映射结构
      const map = {
        0: "未发布",
        1: "活动中",
        2: "已结束",
      };
      return map[type];
    },
    getLocalTime(nS) {
      return new Date(parseInt(nS) * 1000)
        .toLocaleString()
        .replace(/:\d{1,2}$/, " ");
    },
  },
};
</script>
<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
